/**
 * 
 */
package com.personal.common.util;

/**
 * @author Administrator
 * 
 */
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.Properties;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;

/**
 * 读取properties文件
 * 
 * @author Qutr
 * 
 */
public class Configuration {
	private static Logger log = LoggerFactory.getLogger(Configuration.class);
	private final Properties properties;

	private InputStream inputFile;

	private OutputStream outputFile;

	/** */
	/**
	 * 初始化Configuration类
	 */
	public Configuration() {

		this.properties = new Properties();
	}

	/**
	 * 初始化Configuration类
	 * 
	 * @param filePath
	 *            要读取的配置文件的路径+名称
	 */
	public Configuration(String filePath) {

		this.properties = new Properties();
		BufferedReader newBf = null;
		try {
			this.inputFile = Configuration.class.getResourceAsStream(filePath);
			BufferedReader bf = new BufferedReader(new InputStreamReader(inputFile, "UTF-8"));
			this.properties.load(bf);
			this.inputFile.close();
			String path = this.getValue("cloud2.filePath");
			if (StringUtils.isNotBlank(path)) {
				Resource rs = new FileSystemResource(path);
				newBf = new BufferedReader(new InputStreamReader(rs.getInputStream(), "UTF-8"));
				this.properties.load(newBf);
			}
		} catch (FileNotFoundException ex) {
			log.info("读取属性文件--->失败！- 原因：文件路径错误或者文件不存在");
			ex.printStackTrace();
		} catch (IOException ex) {
			log.info("装载文件--->失败!");
			ex.printStackTrace();
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			try {
				if (newBf != null) {
					newBf.close();
				}
				if (inputFile != null) {
					inputFile.close();
				}
			} catch (IOException e) {
			}
		}
	}// end ReadConfigInfo()

	/**
	 * 重载函数，得到key的值
	 * 
	 * @param key
	 *            取得其值的键
	 * @return key的值
	 */
	public String getValue(String key) {
		//log.info("properties.toString()=" + properties.toString());
		if (this.properties.containsKey(key)) {
			String value = this.properties.getProperty(key);// 得到某一属性的值
			return value;
		} else {
			return "";
		}
	}// end getValue()

	/**
	 * 重载函数，得到key的值
	 * 
	 * @param fileName
	 *            properties文件的路径+文件名
	 * @param key
	 *            取得其值的键
	 * @return key的值
	 */
	public String getValue(String fileName, String key) {

		try {
			String value = "";
			this.inputFile = new FileInputStream(fileName);
			this.properties.load(this.inputFile);
			this.inputFile.close();
			if (this.properties.containsKey(key)) {
				value = this.properties.getProperty(key);
				return value;
			} else {
				return value;
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			return "";
		} catch (IOException e) {
			e.printStackTrace();
			return "";
		} catch (Exception ex) {
			ex.printStackTrace();
			return "";
		}
	}// end getValue()

	/**
	 * 清除properties文件中所有的key和其值
	 */
	public void clear() {

		this.properties.clear();
	}// end clear();

	/**
	 * 改变或添加一个key的值，当key存在于properties文件中时该key的值被value所代替， 当key不存在时，该key的值是value
	 * 
	 * @param key
	 *            要存入的键
	 * @param value
	 *            要存入的值
	 */
	public void setValue(String key, String value) {

		this.properties.setProperty(key, value);
	}// end setValue()

	/**
	 * 将更改后的文件数据存入指定的文件中，该文件可以事先不存在。
	 * 
	 * @param fileName
	 *            文件路径+文件名称
	 * @param description
	 *            对该文件的描述
	 */
	public void saveFile(String fileName, String description) {

		try {
			this.outputFile = new FileOutputStream(fileName);
			this.properties.store(this.outputFile, description);
			this.outputFile.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException ioe) {
			ioe.printStackTrace();
		}
	}// end saveFile()

	public static void main(String[] args) {

		Configuration rc = new Configuration("/config.properties");// 相对路径

		String fileAddr = rc.getValue("file.addr");
		log.info("file.addr = " + fileAddr);

		String exchAddr = rc.getValue("exch.addr");
		log.info("exch.addr = " + exchAddr);

	}// end main()
}// end class ReadConfigInfo
